<?php


namespace App\Repositories;


use App\Exceptions\BasicException;

class GiftTagRepository
{
    public function addOrEdit($param)
    {
        $id = $param['id'] ?? 0;
        $tagName = $param['tag_name'];
        $bgColor = $param['bg_color'];
        $status = $param['status'];

        if (!preg_match("/^#[0-9a-fA-F]{6}$/", $bgColor)) {
            throw new BasicException(0, '背景颜色格式错误');
        }

        $data = [
            'name' => $tagName,
            'bg_color' => $bgColor,
            'status' => $status,
        ];

        if ($id > 0) {
            $giftTagId = app('model_gift_tag')->where('id', $id)->value('id');
            if (empty($giftTagId)) throw new BasicException(0, 'id错误，记录不存在');

            $res = app('model_gift_tag')->where('id', $id)->update($data);
        } else {
            $res = app('model_gift_tag')->insert($data);
        }
        return $res;
    }
}